package com.csust.automaticteach.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csust.automaticteach.constant.OrderConstant;
import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.exception.ThrowUtils;
import com.csust.automaticteach.model.dto.classes.ClassesQueryRequest;
import com.csust.automaticteach.model.dto.classes.classExercise.AddClassExerciseRequest;
import com.csust.automaticteach.model.entity.ClassExercise;
import com.csust.automaticteach.model.entity.Classes;
import com.csust.automaticteach.model.vo.classes.ClassesVO;
import com.csust.automaticteach.service.ClassExerciseService;
import com.csust.automaticteach.service.ClassesService;
import com.csust.automaticteach.mapper.ClassesMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 钰玟
* @description 针对表【t_classes(班级表)】的数据库操作Service实现
* @createDate 2025-07-20 11:19:51
*/
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes>
    implements ClassesService{
    @Resource
    private ClassExerciseService classExerciseService;


    /**
     * 获取查询条件
     *
     * @param classesQueryRequest 查询条件
     * @return 查询条件
     */
    @Override
    public QueryWrapper<Classes> getClassesQueryWrapper(ClassesQueryRequest classesQueryRequest) {
        //1. 获取查询条件
        Long id = classesQueryRequest.getId();
        String headTeacher = classesQueryRequest.getHeadTeacher();
        Integer status = classesQueryRequest.getStatus();
        String sortField = classesQueryRequest.getSortField();
        String sortOrder = classesQueryRequest.getSortOrder();
        //2. 构建查询条件
        QueryWrapper<Classes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjUtil.isNotNull(id), "id", id);
        queryWrapper.eq(ObjUtil.isNotNull(headTeacher), "headTeacher", headTeacher);
        queryWrapper.eq(ObjUtil.isNotNull(status), "status", status);
        queryWrapper.orderBy(StrUtil.isNotBlank(sortField), OrderConstant.ASC.equals(sortOrder), sortField);
        //返回query
        return queryWrapper;
    }

    /**
     * 获取班级下的所有考试
     *
     * @param Id 班级id
     * @return List<Long>
     */
    @Override
    public List<Long> getClassExamIds(Long Id) {
        //构建查询条件
        QueryWrapper<ClassExercise> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classId", Id);
        //获取班级下的所有考试
        List<ClassExercise> classExercises = classExerciseService.list(queryWrapper);
        //获取考试id列表
        if (CollUtil.isNotEmpty(classExercises)) {
            return classExercises.stream().map(ClassExercise::getExerciseId).toList();
        }
        return List.of();
    }

    /**
     * 在班级下添加考试
     *
     * @param addClassExerciseRequest 添加的参数
     * @return 添加的id
     */
    @Override
    public Long addClassExam(AddClassExerciseRequest addClassExerciseRequest) {
        //校验参数
        ThrowUtils.throwIf(addClassExerciseRequest == null, ErrorCode.PARAMS_ERROR);
        ClassExercise classExercise = new ClassExercise();
        BeanUtils.copyProperties(addClassExerciseRequest, classExercise);
        boolean saved = classExerciseService.save(classExercise);
        ThrowUtils.throwIf(!saved, ErrorCode.OPERATION_ERROR);
        return classExercise.getId();
    }

    /**
     * 获取班级vo列表
     *
     * @param records 实体类
     * @return List<ClassesVO>
     */
    @Override
    public List<ClassesVO> getClassesVOList(List<Classes> records) {
        return records.stream().map(ClassesVO::objToVo).toList();
    }
}




